package com.example.websocketdemo.websocketConfig;

import com.example.websocketdemo.pojo.ServerMongodb;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;
import java.io.IOException;
import java.util.List;

/**
 * TODO   这是一个编码器
 *  该类的主要作用是，当发送的消息是对象时，该如何转换
 * @ClassName EncoderClassVo
 * @Author xiaojiang
 * @Date 2021/9/27 下午 18:10
 */
public class EncoderClassVo implements Encoder.Text<List<ServerMongodb>>{

    @Override
    public String encode(List<ServerMongodb> map) throws EncodeException {
        ObjectMapper mapMapper= new ObjectMapper();
        try {
            String json="";
            json=mapMapper.writeValueAsString(map);
            return  json;
        } catch (IOException e) {
            e.printStackTrace();
            return "false";
        }
    }



    @Override
    public void init(EndpointConfig config) {

    }

    @Override
    public void destroy() {

    }
}
